package com.xiaoge.order;

import org.springframework.core.OrderComparator;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.Order;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO 实现spring排序接口
 *
 * @author <a href="mailto:1330137071@qq.com">Zhang Xiao</a>
 * @since
 */
//@Order(1)
public class OrderService implements Ordered {

	private int order;

	@Override
	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	@Override
	public String toString() {
		return "OrderService{" +
				"order=" + order +
				'}';
	}

	public static void main(String[] args) {
		OrderService orderService1 = new OrderService();
		OrderService orderService2 = new OrderService();
		OrderService orderService3 = new OrderService();

		orderService1.setOrder(1);
		orderService2.setOrder(3);
		orderService3.setOrder(2);

		List<OrderService> list = new ArrayList<>();
		list.add(orderService1);
		list.add(orderService2);
		list.add(orderService3);

		// todo OrderComparator 重点 它是排序Ordered接口, 根据Ordered接口里面的order值进行排序
		// list.sort(new OrderComparator());
		// todo AnnotationAwareOrderComparator是OrderComparator的子类有它的功能, 并且如果你没实现ordered接口, 它会去看你有没有用@Order注解,进行排序
		list.sort(new AnnotationAwareOrderComparator());

		System.out.println(list);
	}
}
